<template>
  <a-modal
    title="编辑用户"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="昵称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['userName', {initialValue: user.userName, rules: [{required: true, message: '请输入你的姓名', trigger: 'blur'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="用户角色"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select v-decorator="['roleId', {initialValue: String(user.roleId), rules: [{required: true, message: '请选择角色'}]}]" :disabled="flag" placeholder="请选择">
                <a-select-option v-for="role in roles" :key="role.id">{{ role.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12" style="display:none;">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="用户名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input style="display:none;"
                v-decorator="['loginName', {initialValue: user.loginName, rules: [
                  { required: true, message: '请输入账号用户名', trigger: 'blur' },
                  { type: 'string', min: 2, message: '账号用户名不允许小于2位', trigger: 'blur' }]}]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">

        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { editUser } from '@/api/basic/user'
import { listUnitTree } from '@/api/basic/unit'
import { TreeSelect } from 'ant-design-vue'

export default {
  name: 'EditUser',
  components: {
    ATreeSelect: TreeSelect
  },
  props: {
    roles: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      expandedKeys: [1],
      treeData: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      user: {},
      flag: this.$store.getters.userInfo.id !== 1,
      form: this.$form.createForm(this)
    }
  },
  created () {
    listUnitTree()
      .then(res => {
        if (res.message !== 'error') {
          this.treeData = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    edit (record) {
      this.visible = true
      this.user = record
    },
    // 前端验证手机号码
    validatePhone (rule, value, callback) {
      const reg = /^[1][3-9][0-9]{9}$/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if ((!reg.test(value)) && value !== '') {
          callback(new Error('请输入正确的电话号码'))
        } else {
          callback()
        }
      }
    },
    // 前端验证邮箱
    validateEmail (rule, value, callback) {
      const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的邮箱'))
        } else {
          callback()
        }
      }
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          editUser(Object.assign(values, { 'userId': this.user.userId, 'userType': this.user.userType }))
            .then(res => {
              if (res.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
